/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.rukopisyimport.cli;

import cz.mzk.kramerius.commons.Dom4jUtils;
import cz.mzk.kramerius.commons.PropertyLoader;
import cz.mzk.kramerius.commons.Utils;
import cz.mzk.kramerius.fedoraaccess.FedoraConnector;
import cz.mzk.kramerius.fedoraaccess.FedoraServer;
import cz.mzk.kramerius.fedoraaccess.rest.RESTConnector;
import cz.mzk.kramerius.rukopisyimport.ManuscriptCollection;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.Document;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class ImportFoxmlFilesAction implements Action {

    private static final Logger logger = Logger.getLogger(ImportFoxmlFilesAction.class.getName());
    private static final String CONVERTED_DIR = "Convertor.convertedDir";
    private final ManuscriptCollection collection;
    private final String signature;
    private final Properties properties;

    public ImportFoxmlFilesAction(ManuscriptCollection collection, String signature, Properties properties) {
        this.collection = collection;
        this.signature = signature;
        this.properties = properties;
    }

    public ActionResult run(ActionResult previousActionResult) {
        try {
            File foxmlDir = buildManuscriptDir(collection, signature, properties);
            FedoraServer fedora = createFedoraServer(properties);
            FedoraConnector con = new RESTConnector(fedora);
            logger.log(Level.INFO, "dir with xml files: {0}", foxmlDir.getAbsolutePath());
            for (File file : listXmlFiles(foxmlDir)) {
                logger.log(Level.INFO, "loading file {0}", file.getAbsolutePath());
                Document doc = Dom4jUtils.loadDocument(file, false);
                con.importFoxml(doc, "importing manuscript " + signature);
                logger.log(Level.INFO, "file ingested");
                Utils.busyWait(3);
            }
            return new ActionResult(true, previousActionResult.getResultData());
        } catch (Exception ex) {
            logger.log(Level.SEVERE, null, ex);
            return new ActionResult(false, previousActionResult.getResultData());
        }
    }

    private File[] listXmlFiles(File dir) {
        return dir.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".xml");
            }
        });
    }

    private File buildManuscriptDir(ManuscriptCollection collection, String signature, Properties properties) {
        String dirName = getConvertedDir(properties).getAbsolutePath()
                + File.separator
                + collection.getName()
                + File.separator
                + signature;
        return new File(dirName);
    }

    private static File getConvertedDir(Properties properties) {
        PropertyLoader loader = new PropertyLoader(properties);
        return loader.loadDir(CONVERTED_DIR, true, false, true);
    }

    private FedoraServer createFedoraServer(Properties properties) {
        PropertyLoader loader = new PropertyLoader(properties);
        String url = loader.loadString("Fedora.url");
        String login = loader.loadString("Fedora.login");
        String password = loader.loadString("Fedora.password");
        return new FedoraServer(url, login, password);
    }
}
